package com.wang.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.yygh.common.result.Result;
import com.wang.yygh.common.utils.MD5;
import com.wang.yygh.model.hosp.HospitalSet;
import com.wang.yygh.service.HospitalSetService;
import com.wang.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //解决跨域问题，允许跨域
public class HospitalSetController {
    //注入Serviice
    @Autowired
    private HospitalSetService hospitalSetService;

    //test
    @ApiOperation(value = "查询所有医院")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return  Result.ok(list);
    }

    //delete
    @ApiOperation(value = "逻辑删除医院")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return  Result.ok(flag);
        }else {
            return  Result.fail();
        }
    }

    //条件查询带分页,以JSON的形式从前端请求数据
    @ApiOperation(value = "分页条件查询医院")
    @PostMapping("/findPage/{current}/{limit}")
    public  Result findPageHospSet(@PathVariable long current,
                                   @PathVariable long limit,
                                   @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo)//required = false 表示hospitalSetQueryVo这个值可以不用
    {
        Page<HospitalSet> page = new Page<>(current,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(hospitalSetQueryVo.getHosname()),"hosname",hospitalSetQueryVo.getHosname());
        queryWrapper.eq(StringUtils.isNotBlank(hospitalSetQueryVo.getHoscode()),"hoscode",hospitalSetQueryVo.getHoscode());
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page,queryWrapper);
        return  Result.ok(pageHospitalSet);
    }

    //增加医院设置
    @ApiOperation(value = "增加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态： 1可用 0不可用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        return save ? Result.ok():Result.fail();
    }


    //根据ID获取医院信息
    @ApiOperation(value = "根据ID获取医院信息")
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return  Result.ok(hospitalSet);
    }

    //修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById){
            return Result.ok();
        }else{
            return  Result.fail();
        }
    }

    //批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean removeByIds = hospitalSetService.removeByIds(idList);
        return  Result.ok();
    }

    //医院设置的锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet( @PathVariable Long id,
                                   @PathVariable Integer status)
    {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? Result.ok():Result.fail();
    }


    //发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result sendKey( @PathVariable Long id)
    {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO:发送短信
        return  Result.ok();
    }

}
